package com.flyme.module.base.provider.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysChangecertificateRemoteService;
import com.flyme.module.base.client.dto.SysChangecertificateDto;
import com.flyme.module.base.client.entity.SysChangecertificate;
import com.flyme.module.base.client.entity.SysUsercertificate;
import com.flyme.module.base.provider.service.SysChangecertificateService;
import com.flyme.module.base.provider.service.SysUsercertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * 申请用户证书转换证明管理
 * @author zyh
 * @date 2020-8-19
 */
@Api(tags = "申请用户证书转换证明")
@RestController
public class SysChangecertificateController extends BaseController<SysChangecertificateService, SysChangecertificate> implements SysChangecertificateRemoteService {

    @Autowired
    private SysUsercertificateService usercertificateService;

    /**
     * 审核转换证明
     * @return
     */
    @ApiOperation(value = "发放证书转换证明", notes = "发放证书转换证明")
    @Override
    public ResultBody changeSetStatus(SysChangecertificate changecertificate, String validityTimeString) {
        if(changecertificate.getAuditState().equals("3")){
            boolean flag = bizService.saveOrUpdate(changecertificate);
            return ResultBody.success(flag);
        }
        if(ObjectUtils.isNotEmpty(validityTimeString)){
            DateTime validityTime = DateUtil.parseDateTime(validityTimeString);
            changecertificate.setValidityTime(validityTime);
        }
        if(changecertificate.getAuditState().equals("2")){
            changecertificate.setIsUse(0);
        }
        boolean result= bizService.saveOrUpdate(changecertificate);
        if(result && changecertificate.getAuditState().equals("2")){
            UpdateWrapper certificate = new UpdateWrapper();
            usercertificateService.update(certificate.set(true, "changeStatus", 2).eq(true, "id", changecertificate.getSysUsercertificateId()));
        }
        return ResultBody.success(result);
    }

    /**
     * 查询转换证明申请记录
     */
    @Override
    public ResultBody changeList(PagerInfo pagerInfo, SysChangecertificateDto changecertificate) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*, users.userName,users.idCard,users.schoolNum,users.mobile,syscertificate.certificateName," +
            "certificate.certificateNum,certificate.award,certificate.createTime cuTime,certificate.validity");
        cq.eq("auditState", changecertificate.getAuditState());
        if(ObjectUtils.isNotEmpty(changecertificate.getIsUse()) && !changecertificate.getIsUse().equals(2)){
            cq.eq("isUse", changecertificate.getIsUse());
        }
        cq.like("users.userName", changecertificate.getUserName());
        cq.createAlias("users", "users");
        cq.createAlias("certificate", "certificate");
        cq.createAlias("syscertificate", "syscertificate");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

}
